layui.use(['jquery', 'laypage', 'layer'], function () {
    let $ = layui.jquery,
        laypage = layui.laypage,
        layer = layui.layer;
    if (localStorage.getItem('Authorization')) {
        let text = '<a href="#" style="color: white">' + localStorage.getItem('name') + '</a>'
        $('.navbar-right li').eq(1).html(text)
        $('#photo').attr('src', localStorage.getItem('photo'))
    } else {
        layer.alert('请先登录', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            , closeBtn: 0    // 是否显示关闭按钮
            , anim: 1 //动画类型
            , btn: ['返回主页'] //按钮
            , icon: 6    // icon
            , yes: function () {
                location.href = '/'
            }
        });
    }
    let count = 0
    let countAjax = $.ajax({
        type: "GET",
        url: '/api/article/count',
        dataType: 'json',
        data: {
            id: getUrlParam("id"),
            classify: getUrlParam('classify')
        },
        success: function (data) {
            if (data.status === 200) {
                count = data.data.count
            } else {
                layer.msg(data.msg)
            }
        },
        err: function () {
            layui.use('layer', function () {
                layer.msg("服务器请求失败")
            });
        }
    });
    $.when(countAjax).done(function () {
        laypage.render({
            elem: 'paging'
            , count: count
            , limit: 3
            , jump: function (obj) {
                $.ajax({
                    type: "GET",
                    url: '/api/article/listArticle',
                    dataType: 'json',
                    data: {
                        id: getUrlParam("id"),
                        classify: getUrlParam('classify'),
                        page: obj.curr,
                        limit: 3
                    },
                    success: function (data) {
                        if (data.status === 200) {
                            data = data.data
                            let html = ""
                            for (let i = 0; i < data.length; ++i) {
                                let text =
                                    ' <div class="post-item">\n' +
                                    '                    <h1>\n' +
                                    '                        <a href="/article?id=' + data[i].id + '">\n' +
                                    '                            ' + data[i].title +
                                    '                        </a>\n' +
                                    '                    </h1>\n' +
                                    '                    <div class="create">\n' +
                                    '                        <span>Created</span>\n' +
                                    '                        <time>\n' +
                                    '                            ' + data[i].date + '\n' +
                                    '                        </time>\n' +
                                    '                    </div>\n' +
                                    '                   ' + data[i].content +
                                    '                    <div class="readmore">\n' +
                                    '                        <a href="/article?id=' + data[i].id + '">\n' +
                                    '                            Read More\n' +
                                    '                        </a>\n' +
                                    '                        &emsp;\n' +
                                    '                        <a href="/edit?id=' + data[i].id + '">Edit</a>' +
                                    '                        &emsp;\n' +
                                    '                        <input type="text" value="' + data[i].id + '" style="display: none ">' +
                                    '                        <a href="#" class="delete-article">Delete</a>' +
                                    '                    </div>\n' +
                                    '                </div>'
                                html += text
                            }
                            $('#recent-posts').html(html)
                        } else {
                            layer.msg(data.msg)
                        }
                    },
                    err: function () {
                        layui.use('layer', function () {
                            layer.msg("服务器请求失败")
                        });
                    }
                });
            }
        });
    })
    $(document).on('click', '.delete-article', function () {

        let id = $(this).prev().val()
        console.log(id)
        layer.alert('确定删除？', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            , closeBtn: 1    // 是否显示关闭按钮
            , anim: 1 //动画类型
            , btn: ['确定', '取消'] //按钮
            , icon: 6    // icon
            , yes: function () {
                $.ajax({
                    type: "POST",
                    url: '/api/article/delArticle',
                    dataType: 'json',
                    headers: {
                        'Authorization': localStorage.getItem('Authorization')
                    },
                    data: {
                        id: id
                    },
                    success: function (data) {
                        if (data.status === 200) {
                            layer.msg('删除成功')
                            setTimeout(function () {
                                window.location.reload()
                            }, 1000);
                        } else {
                            layer.msg(data.msg)
                        }
                    },
                    err: function () {
                        layui.use('layer', function () {
                            layer.msg("服务器请求失败")
                        });
                    }
                });
            }
        });
    })
});

function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    let r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}